<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*, myclasses.*, java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Country of residence</title>
</head>
<body>
<%
	Student stud = (Student)session.getAttribute("student");
	String citizenship = request.getParameter("citizenship");
	stud.setCitizenshipId(citizenship);
	String citName = "";
	
	try {
		Class.forName("org.postgresql.Driver");
	} catch(Exception e) {
		out.println("org.postgresql.Driver Exception. ");
	}

	try {
		
	    String serverName = "localhost:5432";
	    String mydatabase = "ThirdBase";
	    String url = "jdbc:postgresql://" + serverName + "/" + mydatabase; 
	    String username = "SweetAss";
	    String password = "1234";

	    Connection conn = DriverManager.getConnection(url, username, password);

		Statement stmt = conn.createStatement();
		ResultSet rset = stmt.executeQuery("SELECT name FROM country WHERE id = " + citizenship);
	
		
		rset.next();
		citName = rset.getString(1);
		
		rset.close();
		stmt.close();
    	conn.close();
    	
	} catch(SQLException e) {
		e.printStackTrace();
		out.println("Connection exception.");
	}
%>
<center>
<!-- PERHAPS MAKE A PRINT METHOD FOR THE STUDENT SO WE DONT HAVE TO WRITE THIS CODE EVERY TIME? -->
<%= stud.getFirstName() %> <%= stud.getMiddleInitial() %>. <%= stud.getLastName() %> you chose <%= citName %> as your country of citizenship.<br>
Please choose your country of residence:<br>
</center>

<%	out.println("<center>");
	out.println("<a href=\"http://localhost:8080/GradSchoolApp/address.jsp?residence=" + citizenship + "\">" + "Same as country of citizenship" + "</a>");
	out.println("</center>");

	try {
		Class.forName("org.postgresql.Driver");
	} catch(Exception e) {
		out.println("org.postgresql.Driver Exception. ");
	}

	try {
		String serverName = "localhost:5432";
	    String mydatabase = "ThirdBase";
	    String url = "jdbc:postgresql://" + serverName + "/" + mydatabase; 
	    String username = "SweetAss";
	    String password = "1234";
	    Connection conn = DriverManager.getConnection(url, username, password);
	    
		Statement stmt = conn.createStatement();

		ResultSet rset = stmt.executeQuery("SELECT id,name FROM country");
	
		Integer columncounter = 0;
		out.println("<br>");
		out.println("<table width=\"600\" border=\"2\" cellpadding=\"2\" cellspacing=\"2\" align=\"center\">");
		
		while(rset.next()) {
			columncounter += 1;
			if(columncounter == 1)
			out.println("<tr>");
		
			out.println("<td>" + "<a href=\"http://localhost:8080/GradSchoolApp/address.jsp?residence=" + rset.getString(1) + "\">" + rset.getString(2) + "</a>" + "</td>");
		
			if (columncounter >= 3) {
				out.println("</tr>");
				columncounter = 0;
			}
		}

	out.println("</table>");
	out.println("<br>");
	
	
	rset.close();
	stmt.close();
    conn.close();
    
} catch(SQLException e) {
	e.printStackTrace();
	out.println("Connection exception.");
}
	
%>

</body>
</html>